package com.pk.platform.business.data.service.impl;

import java.util.Date;

import com.pk.platform.business.data.dao.IMasterDataDao;
import com.pk.platform.business.data.service.IMasterDataService;
import com.pk.platform.domain.data.Employee;
import com.pk.platform.domain.privilege.User;
import com.pk.platform.util.StringConverter;
import com.pk.platform.util.constant.Constant;
import com.pk.platform.util.filter.SystemContext;

public class MasterDataServiceImpl implements IMasterDataService {

	private IMasterDataDao masterDataDao;
	
	@Override
	public synchronized String saveEmployee(Employee emp) {
		String success = StringConverter.resultStr(Constant.SUCCESS, null);
		String failure = StringConverter.resultStr(Constant.FAILURE, "已存在该员工编号，请重新输入。");
		String result = success;
		int count = masterDataDao.queryEmployeeCountByNum(emp.getEmployeeNum(), emp.getId());
		if(StringConverter.isNotNull(emp.getId()))
		{
			if(count > 0)
			{
				result = failure;
			}
			else
			{
				Employee employee = this.queryEmployeeById(emp.getId());
				employee.setEmployeeName(emp.getEmployeeName());
				employee.setEmployeeNum(emp.getEmployeeNum());
				employee.setSex(emp.getSex());
				employee.setJob(emp.getJob());
				employee.setBirthday(emp.getBirthday());
				employee.setLastModifyTime(new Date());
			}
		}
		else
		{
			if(count > 0)
			{
				result = failure;
			}
			else
			{
				User creator = SystemContext.getUser();
				emp.setCreator(creator);
				masterDataDao.add(emp);
			}
		}
		return result;
	}

	@Override
	public void delEmployee(String id) {
		masterDataDao.delete(Employee.class, id);
	}

	@Override
	public Employee queryEmployeeById(String id) {
		return masterDataDao.get(Employee.class, id);
	}

	public IMasterDataDao getMasterDataDao() {
		return masterDataDao;
	}

	public void setMasterDataDao(IMasterDataDao masterDataDao) {
		this.masterDataDao = masterDataDao;
	}

}
